//
//  SwipeUserNameViewController.m
//  LFCWarmSystem
//
//  Created by Kill on 16/4/29.
//  Copyright © 2016年 李福才. All rights reserved.
//

#import "SwipeUserNameViewController.h"
#import "TwoAndThreeTableViewCell.h"

@interface SwipeUserNameViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,MFMessageComposeViewControllerDelegate>{
    BOOL isEdited;
    UITableView *myTableView;
}
@property (nonatomic,strong,readwrite)NSArray *placeholderArr;
@end

@implementation SwipeUserNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self uiInit];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

-(void)uiInit{
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIView *mainView = [[UIScrollView alloc] initWithFrame:CGRectMake(10*WIDTHSCALE, 64+20*WIDTHSCALE, self.view.frame.size.width-2*10*WIDTHSCALE, self.view.frame.size.height-64-20*WIDTHSCALE-64*WIDTHSCALE)];
    mainView.layer.cornerRadius = 5.0;
    mainView.backgroundColor = BACKGROUNDVIEWCOLOR;
    [self.view addSubview:mainView];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:mainView.bounds];
    tableView.tableFooterView = [[UIView alloc] init];
    tableView.backgroundColor = TABLEVIEWCOLOR;
    tableView.layer.cornerRadius = 5.0;
    tableView.rowHeight = TABLEVIEWCELLHEIGHT;
    [tableView setSeparatorColor:[UIColor clearColor]];
    tableView.delegate = self;
    tableView.dataSource = self;
    [mainView addSubview:tableView];
    myTableView = tableView;
    
    UILabel *lblHeadView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, tableView.frame.size.height/8.0)];
    lblHeadView.text = NSLocalizedString(@"SwipeUser_Sample",@"刷卡用户名");
    lblHeadView.textColor = COLOR_MAINHEAD;
    lblHeadView.font = TABLEHEADFONT;
    lblHeadView.textAlignment = NSTextAlignmentCenter;
    [tableView setTableHeaderView:lblHeadView];
    _placeholderArr = @[NSLocalizedString(@"RFIDTagName01", @"  xxxx到家"),
                        NSLocalizedString(@"RFIDTagName02", @"  xxxx到家"),
                        NSLocalizedString(@"RFIDTagName03", @"  xxxx到家"),
                        NSLocalizedString(@"RFIDTagName04", @"  xxxx到家")];
    [mainView addSubview:myTableView];
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"FastCallCell";
    TwoAndThreeTableViewCell *cell = (TwoAndThreeTableViewCell *)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
        cell = [[TwoAndThreeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell.textField setHidden:NO];
        cell.textField.delegate = self;
    }
    cell.textField.placeholder = _placeholderArr[indexPath.row];
    
    return cell;
}
-(void)rightAction:(id)sender{
    NSString *msgContect=[self languageMSG:@"Change FRID tagsSMS notice:"];
//    if (isEdited) {
        for (int i=0; i<4; i++) {
            TwoAndThreeTableViewCell *cell = [myTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
            NSString *string=[NSString stringWithFormat:@"\n%d.%@",i+1,nil!=cell.textField.text?cell.textField.text:@""];
            msgContect = [msgContect stringByAppendingString:string];
        }
//    }
    [self showSMSPicker:msgContect];
}
- (NSString *)languageMSG:(NSString*)content{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"mulLan" ofType:@"plist"];
    NSDictionary *data = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    NSString *str = @"";
    if ((str = [data objectForKey:[NSString stringWithFormat:@"CChangeRFIDNoticeSMS_%@ ",self.modelPhone.language]])) {
        content = str;
        
    }
    return [[content componentsSeparatedByString:@"\\r"] firstObject];
}

#pragma mark-Send Message
-(void)showSMSPicker:(NSString *)MSGContent{
    Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
    if (messageClass != nil) {
        // Check whether the current device is configured for sending SMS messages
        if ([messageClass canSendText]) {
            MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
            picker.recipients = [NSArray arrayWithObjects:self.modelPhone.phoneNumber, nil];
            picker.messageComposeDelegate = self;
            picker.body = MSGContent;
            [self presentViewController:picker animated:YES completion:^{
            }];
        }
        else {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"DeviceCantMSG", @"设备没有短信功能")  message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
            [alert show];
        }
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"VersionProblem", @"iOS版本过低,iOS4.0以上才支持程序内发送短信") message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
        [alert show];
    }
}

//#pragma mark-MFMessageComposeViewControllerDelegate
//- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
//    
//    switch (result)
//    {
//        case MessageComposeResultCancelled:
//            break;
//        case MessageComposeResultSent:
//            break;
//        case MessageComposeResultFailed:{
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"SendMSGFail", @"短信发送失败")  message:nil delegate:nil cancelButtonTitle:NSLocalizedString(@"Close_Sample", @"  关闭") otherButtonTitles: nil];
//            [alert show];
//        }
//            break;
//        default:
//            break;
//    }
//    [self dismissViewControllerAnimated:YES completion:^{}];
//}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    isEdited = YES;
    return YES;
}



#pragma mark - keyboardHight

//实现当键盘出现的时候计算键盘的高度大小。用于输入框显示位置
- (void)keyboardWasShown:(NSNotification*)aNotification {
    NSDictionary* info = [aNotification userInfo];
    //kbSize即為鍵盤尺寸 (有width, height)
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;//得到鍵盤的高度
    NSLog(@"keyboard Height:%f",kbSize.height);
    //输入框位置动画加载
    [self begainMoveUpAnimation:kbSize.height];
}
//当键盘隐藏的时候
- (void)keyboardWillBeHidden:(NSNotification*)aNotification {
    //do something
    [self begainMoveUpAnimation:0];
}
- (void)begainMoveUpAnimation:(CGFloat)keyBoardHeight {
    //开始动画
    [UIView beginAnimations:nil context:nil];
    //设定动画持续时间
    [UIView setAnimationDuration:0.5];
    //动画的内容
    NSLog(@"self.view %f,%f",self.view.frame.size.width,self.view.frame.size.height);
    CGRect frame = CGRectMake(0, 0, self.view.frame.size.width-2*10*WIDTHSCALE, self.view.frame.size.height-64-20*WIDTHSCALE-10-(keyBoardHeight>1?keyBoardHeight:64*WIDTHSCALE));
    [myTableView setFrame:frame];
    
    //动画结束
    [UIView commitAnimations];
}

@end
